import axios from 'axios'
import { ElMessage } from 'element-plus'

import { getToken } from '@/utils/webStorage'

import { useGlobalStore } from '@/stores/globalStore'
import { useRouter, useRoute } from 'vue-router'
import { nextTick } from 'vue'

const router = useRouter()
const route = useRoute()

export const baseURL = hosts.DATA_URL[import.meta.env.MODE] // 在host.js中配置

const service = axios.create({
  baseURL: `${baseURL}/app`,
  // baseURL: '/app',
  timeout: 1000 * 30, // 超时响应
})

service.interceptors.request.use((config) => {
  config.headers.token = getToken()
  return config
})

service.interceptors.response.use(
  (response) => {
    if (response && response.data.code === 500) {
      if (response.data.msg !== '服务器出小差啦' && response.data.msg !== '您没有关注的用户') {
        ElMessage({
          message: response.data.msg,
          type: 'error',
        })
      }
      return Promise.reject(response.data.msg)
    }
    if (response && response.data.code === 401) {
      // console.log('response.data.msg', response.data.msg)
      if (response.data.msg === 'token不能为空' || response.data.msg === 'token失效，请重新登录') {
        const authStore = useGlobalStore()
        authStore.showLogin()
        ElMessage({
          message: 'token失效，请重新登录',
          type: 'error',
        })
        return
      }
    }
    if (response && response.data.code === 410) {
      // console.log('response.data.msg', response.data.msg)
      ElMessage({
          message: response.data.msg,
          type: 'error',
        })
        return response.data
        // nextTick(() => {
        //      console.log('router.currentRoute=====');
        // console.log(router);
        // console.log(route);
        //   //判断当前路由如果包含home/post
        //   if (router.currentRoute.value.path.includes('/home/post')) {
        //     //返回上一级页面
        //     // router.back()
        //     router.push('/home')
        //   } else {
        //     router.push('/expert')
        //   }
        // })

        

        // //判断当前路由如果包含home/post
        // if (router.currentRoute.value.path.includes('/home/post')) {
        //   //返回上一级页面
        //   // router.back()
        //   router.push('/home')
        // } else {
        //   router.push('/expert')
        // }
    }
    return response.data
  },
  (error) => {
    // 检查错误响应的状态码
    console.log('error', error)
    // if (error.response && error.response.status === 401) {
    //   // 弹出登录页提示框
    //   ElMessageBox.confirm('登录已过期，请重新登录', '未授权', {
    //     confirmButtonText: '去登录',
    //     cancelButtonText: '取消',
    //     type: 'warning',
    //   })
    //     .then(() => {
    //       // 如果用户点击确认，跳转到登录页
    //       router.push('/login') // 替换为实际的登录路由
    //     })
    //     .catch(() => {
    //       // 如果用户点击取消，可以添加取消操作的逻辑
    //       console.log('用户取消了重新登录')
    //     })
    // }
    return Promise.reject(error)
  },
)

export const get = (url, params) => {
  return service({
    url,
    method: 'get',
    params,
  })
}

export const post = (url, data) => {
  return service({
    url,
    method: 'post',
    data,
  })
}

export const put = (url, data) => {
  return service({
    url,
    method: 'put',
    data,
  })
}

export const del = (url, data) => {
  return service({
    url,
    method: 'delete',
    data,
  })
}

export default service
